package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.RoomInfo;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.RoomInfoService;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "房间信息管理")
@RestController
@RequestMapping("/admin/room")
public class RoomController {

    @Autowired
    RoomInfoService roomInfoService;

    @Operation(summary = "保存或更新房间信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody RoomSubmitVo roomSubmitVo) {
        roomInfoService.saveOrUpdateRoom(roomSubmitVo);
        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询房间列表")
    @GetMapping("pageItem")
    public Result<IPage<RoomItemVo>> pageItem(@RequestParam long current, @RequestParam long size, RoomQueryVo queryVo) {
        //这里直接就用Vo了，不用roomInfo转Vo了   （注意有的地方要转）
        IPage<RoomItemVo> roomItemVoPage = new Page<>(current,size);
        IPage<RoomItemVo> pageResult = roomInfoService.pageItem(roomItemVoPage,queryVo);
        return Result.ok(pageResult);
    }

    @Operation(summary = "根据id获取房间详细信息")
    @GetMapping("getDetailById")
    public Result<RoomDetailVo> getDetailById(@RequestParam Long id) {
        RoomDetailVo roomDetailVo = roomInfoService.getRoomDetailById(id);
        return Result.ok(roomDetailVo);
    }

    @Operation(summary = "根据id删除房间信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        roomInfoService.removeRoomById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id修改房间发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(Long id, ReleaseStatus status) {
        LambdaUpdateWrapper<RoomInfo> roomInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        roomInfoLambdaUpdateWrapper.eq(RoomInfo::getId,id);
        roomInfoLambdaUpdateWrapper.set(RoomInfo::getIsRelease,status);
        roomInfoService.update(roomInfoLambdaUpdateWrapper);
        return Result.ok();
    }

    @GetMapping("listBasicByApartmentId")
    @Operation(summary = "根据公寓id查询房间列表")
    public Result<List<RoomInfo>> listBasicByApartmentId(Long id) {
        //思路：先把所有已经发布的房间查询出来，再把已经租出去的房间删除
        LambdaQueryWrapper<RoomInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoomInfo::getApartmentId, id);
        queryWrapper.eq(RoomInfo::getIsRelease, ReleaseStatus.RELEASED);
        List<RoomInfo> roomInfoList = roomInfoService.list(queryWrapper);  //该公寓所有的房间信息
        List<Long> leaseRoomIds = roomInfoService.getReleaseRoomByApartmentId(id);   //被租赁的房间号


        //方式一：
//        Iterator<RoomInfo> iterator = roomInfoList.iterator();
//        while (iterator.hasNext()){
//            RoomInfo room = iterator.next();
//            if (leaseRoomIds.contains(room.getId())){
//                iterator.remove();
//            }
//        }
        //方式二：removeIf
//        roomInfoList.removeIf(roomInfo -> leaseRoomIds.contains(roomInfo.getId()));

        //方式三：stream()
        List<RoomInfo> collect = roomInfoList.stream().filter(roomInfo -> !leaseRoomIds.contains(roomInfo.getId())).collect(Collectors.toList());
        //方式四 这种方式不行，会出现空指针异常，集合的索引减少了
//        for (RoomInfo roomInfo : roomInfoList) {
//            for (Long leaseRoomId : leaseRoomIds) {
//                if (leaseRoomId == roomInfo.getId()){
//                    roomInfoList.remove(roomInfo);
//                }
//            }
//        }
        return Result.ok(collect);
    }

}


















